# from common.AnsibleExec import AnsibleExec
# import json, sys, os
# import logging
# from webapp.models import *
# from webapp.serializer import *
#
# class NginxExec():
#     def __init__(self, tid):
#         self._tid = tid
#         self.upstream_dict = {}
#         self.host_dict = {}
#         self.nginx_http_template = {}
#
#     def nginx_http_template(self):
#         nginx_list = NginxTemplate.objects.get(id=self._tid).groups_set.all()
#
#         for nginx in nginx_list:
#             website_query = NginxSerializer(nginx)
#             self.nginx_http_template[website_query.data['template_name']] = website_query.data
#             self.nginx_http_template[website_query.data['template_name']]["reverse_proxy"] = {}
#             self.nginx_http_template[website_query.data['template_name']]["reverse_proxy"]["locations"] = {}
#             self.nginx_http_template[website_query.data['template_name']]["upstreams"] = self.upstream_dict
#             locations = nginx.templates.all()
#             for u in locations:
#                 if u.proxy_type is True:
#                     self.nginx_http_template[website_query.data['template_name']]["reverse_proxy"]["locations"][u.name] = {}
#                     self.nginx_http_template[website_query.data['template_name']]["reverse_proxy"]["locations"][u.name]['location'] = u.location
#                     for upstream in u.locations.all():
#                         self.nginx_http_template[website_query.data['template_name']]["reverse_proxy"]["locations"][u.name]["proxy_pass"] = 'http://' + UpstreamDetailSerializer(upstream).data['name']
#                         self.upstream_dict[(UpstreamDetailSerializer(upstream).data)['name']] = UpstreamDetailSerializer(upstream).data
#                         self.upstream_dict[(UpstreamDetailSerializer(upstream).data)['name']]['servers'] = self.host_dict
#                         for host in upstream.servers.all():
#                             self.host_dict[host.hostname] = HostSerializer(host).data
#         return self.nginx_http_template
#
    # def resource_json(self):
    #     resource={"group1":{"hosts":[],
    #              "groupvars":{
    #                  "nginx_http_template_enable": "true",
    #              }
    #           }
    #        }
    #     resource["group1"]["groupvars"]["nginx_http_template"] = self.nginx_http_template()
    #     return resource
    #
    # def RunPlaybook(self):
    #     print(self.resource_json())
    #     runner = AnsibleExec(self.resource_json())
    #     runner.run_playbook(playbookpath='../ansible/nginx.yaml')
    #     result = runner.results_raw
    #     print(result)


# resource={"group1":{"hosts":
#                      [{"hostid":"123","hostname":"bbc","hostip":"10.42.0.175","k1":"v1"}],
#         "groupvars":{
#   "nginx_http_template_enable": "true",
#     }
#   }
# }